﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.Container;
using Swaf.Gui;
using Swaf.Security;

using eCom;

namespace PayPal.Gui
{

	public class PayPalExpressCheckoutBizProc : BaseBusinessProcess, IPaymentProcessingBizProc
	{
		public PayPalExpressCheckoutBizProc(ActiveBizProc procState) : base(procState) { }

		
		protected override void setupRuleNames()
		{
			//m_getBizObjListRule= "getSalesOrders";
			//m_getBizObjDetailsRule= "getSalesOrderDetails";
			//m_updateBizObjRule = "updateSalesOrder";
			//m_deleteBizObjRule = "deleteSalesOrder";
		}

		public override Privileges CurrentEditPrivilege
		{
			get { return Privileges.Delete; }
		}


		public bool startExpressCheckout()
		{
			IBizObj wizardInfo = m_procState.get("info", false) as IBizObj;
			bool keepProcessingCheckoutEvent = (bool)wizardInfo.get("stepCompleted");
			if (keepProcessingCheckoutEvent)
			{
				IBizObj order = m_procState.get("order") as IBizObj;
				IBizObj contact = m_procState.get("contact") as IBizObj;
				if (order != null && contact != null)
				{
					order.put("orderContact", contact); //I'm thinking this really shouldnt be necessary, but to get the job done it is.
					if ("PayPalPaymentInfo".CompareTo(order.get("paymentMethod.bizObjType")) == 0)
					{
						keepProcessingCheckoutEvent = false;
						IResults rc = App.ruleMgr.execute("startPayPalExpressCheckout", order);
						IBizObj paypalResults = rc.CallResults as IBizObj;
						if (paypalResults != null)
						{
							App.sessionData.put("paypalInfo", paypalResults);
							if (Cmn.IsEmpty(paypalResults.get("error.code")))
							{
								if (!Cmn.IsEmpty(paypalResults.get("firstNav")))
								{
									string palLoc = (string)paypalResults.get("firstNav");
									m_procState.BPC.HostView.gotoView(palLoc);
								}
							}
							else
							{
								FlexiMap m = new FlexiMap();
								m["error"] = paypalResults.get("error");
								m_procState.BPC.Messenger.add("Error-PayPalFailure", m);
							}
						}
					}
				}
			}
			return keepProcessingCheckoutEvent;
		}

		protected override void setupActions()
		{
			base.setupActions();
			Actions.addAction(new GenericAction("StartExpressCheckout",true, true, startExpressCheckout, this));
		}

		public override void updateActionState()
		{
			base.updateActionState();
			bool allow = false;
			if (ActiveCompany.containsField("settings.paypal"))
				allow = (bool)ActiveCompany.get("settings.paypal.allowExpressCheckout");

			((GenericAction)Actions["StartExpressCheckout"]).Availability =
				allow ? ActionAvailability.enabled : ActionAvailability.none;
		}


		#region IPaymentProcessingBizProc Members

		public PaymentProcessResults validatePaymentProcessing()
		{
			PaymentProcessResults prc = PaymentProcessResults.paymentFailure;
			IBizObj order = m_procState.get("order") as IBizObj;
			IBizObj paypallInfo = App.sessionData.get("paypalInfo") as IBizObj;
			if (order != null && paypallInfo != null)
			{
				object didCancel = m_procState.get("Cancel");
				if (didCancel != null && string.Compare("true", didCancel.ToString(), true) == 0)
					prc = PaymentProcessResults.paymentCanceled;
				else
				{
					IResults rc = App.ruleMgr.execute("completePayPalExpressCheckout", order, paypallInfo);
					IBizObj paypalResults = rc.CallResults as IBizObj;
					if (paypalResults != null)
					{
						m_currentBizObj = paypalResults;
						m_retrievedCurrentBizObj = true;
						string ec = (string)paypalResults.get("error.code");
						if (Cmn.IsEmpty(ec))
						{
							prc = PaymentProcessResults.paymentSuccess;
							IBizObj wizardInfo = m_procState.get("info") as IBizObj;
							if (wizardInfo != null)
								wizardInfo.put("stepCompleted", true);
						}
						else if (ec.CompareTo("GoodCancel") == 0)
							prc = PaymentProcessResults.paymentCanceled;
						else
							prc = PaymentProcessResults.paymentFailure;
					}
				}
			}
			return prc;
		}

		public bool isOrderUsingPayment
		{
			get 
			{
				IBizObj paypallInfo = App.sessionData.get("paypalInfo") as IBizObj;
				return paypallInfo != null;
			}
		}

		#endregion
	}
}
